<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Post extends MY_Controller {

	public function __construct() {
		parent::__construct();
		//$this->output->enable_profiler(TRUE);
		$this->_addTitle('Blog');
	}
	
	public function index() {
		$this->browse();	// forward to browse action
	}
	
	public function browse() {
		$this->loadLayout();
		$this->renderLayout();
	}
	
	public function view() {
		$postId = App::segment('id');
		$post = App::object('Entity_Post')->load($postId);
		$this->_addTitle($post->getTitle());

		if (!$post->getId()) {
			throw new Exception('Post not existed.');
			die;
		}
		
		App::registry('post', $post);
		$this->loadLayout();
		$this->renderLayout();
	}
	
	public function update() {
		$post = App::object('Entity_Post');
		$post->setId(App::post('post'));
		$action = App::post('action');

		switch ($action) {
			case 'setStatus':
				$post->setStatus(App::post('status'));
				$post->save();
				echo '1';	// success
				break;

			case 'setRating':
				$post->setRating(App::post('rating'));
				$post->save();
				echo $post->getRating();
				break;

			case 'addTags':
				$post->addTags(App::post('tags'));
				$this->getLayout()->addHandle('POST_RATING');
				$this->loadLayout();
				$this->getLayout()->getBlock('post_view_tag')->setPost($post);
				$this->renderLayout();	// return html
				break;
		}
	}
}
